import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../models/FocusModel.dart';
import '../../../models/category_model.dart';
import '../../../models/plist_model.dart';
import '../../../services/httpsClient.dart';

class HomeController extends GetxController {
  //浮动导航栏的开关
  RxBool flag = false.obs;
  final ScrollController scrollController = ScrollController();
  RxList<FocusItemModel> swiperList = <FocusItemModel>[].obs; //定义成响应式数据
  RxList<CategoryItemModel> categoryList = <CategoryItemModel>[].obs; //定义成响应式数据
  RxList<FocusItemModel> bestSellingSwiperList =
      <FocusItemModel>[].obs; //定义成响应式数据

  RxList<PlistItemModel> sellingPList = <PlistItemModel>[].obs; //定义成响应式数据
  RxList<PlistItemModel> bestPList = <PlistItemModel>[].obs; //定义成响应式数据

  HttpsClient client = HttpsClient();

  @override
  void onInit() {
    super.onInit();
    scrollControllerListener();
    //请求接口服务器数据
    getFocusData();
    //获取分类数据
    getCategoryData();
    //热销甄选里面的轮播
    getSellingSwiperData();
    //获取热销甄选里面的商品
    getSellingPlistData();
    //获取热门商品数据
    getBestPlistData();
  }

  getFocusData() async {
    //var response = await Dio().get("https://miapp.itying.com/api/focus");
    var response = await client.get("api/focus");
    if (response != null) {
      print(response.data is Map);
      print('getFocusData:${response.data}');
      //swiperList.value = response.data['result'];
      var focs = FocusModel.fromJson(response.data);
      swiperList.value = focs.result;
      update();
    }
  }

  //获取热销甄选里面的轮播图
  getSellingSwiperData() async {
    /*  var response =
        await Dio().get("https://miapp.itying.com/api/focus?position=2");*/

    var response = await client.get("api/focus?position=2");
    if (response != null) {
      print(response.data is Map);
      print('getSellingSwiperData:${response.data}');
      //swiperList.value = response.data['result'];
      var bestSelling = FocusModel.fromJson(response.data);
      bestSellingSwiperList.value = bestSelling.result;
      update();
    }
  }

  //获取热销甄选里面的商品数据s
  getBestPlistData() async {
    /*var response =
        await Dio().get("https://miapp.itying.com/api/plist?is_best=1");*/

    var response = await client.get("api/plist?is_best=1");
    if (response != null) {
      print(response.data is Map);
      print('getBestPlistData:${response.data}');
      //swiperList.value = response.data['result'];
      var plist = PlistModel.fromJson(response.data);
      bestPList.value = plist.result!;
      update();
    }
  }

  //获取热销甄选里面的商品数据s
  getSellingPlistData() async {
    /*  var response = await Dio()
        .get("https://miapp.itying.com/api/plist?is_hot=1&pageSize=3");
*/

    var response = await client.get("api/plist?is_hot=1&pageSize=3");
    if (response != null) {
      print(response.data is Map);
      print('getSellingPlistData:${response.data}');
      //swiperList.value = response.data['result'];
      var plist = PlistModel.fromJson(response.data);
      sellingPList.value = plist.result!;
      update();
    }
  }

  getCategoryData() async {
    // var response = await Dio().get("https://miapp.itying.com/api/bestCate");

    var response = await client.get("api/bestCate");
    if (response != null) {
      print(response.data is Map);
      print('getCategoryData:${response.data}');
      //swiperList.value = response.data['result'];
      var category = CategoryModel.fromJson(response.data);
      categoryList.value = category.result!;
      update();
    }
  }

  void scrollControllerListener() {
    scrollController.addListener(() {
      if (scrollController.position.pixels > 10 &&
          scrollController.position.pixels < 20) {
        if (flag.value == false) {
          flag.value = true;
          update();
        }
      }
      if (scrollController.position.pixels < 10) {
        if (flag.value == true) {
          flag.value = false;
          update();
        }
      }
    });
  }
}
